package com.woniu.communitymanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.communitymanagement.entity.House;
import com.woniu.communitymanagement.entity.HttpResult;
import com.woniu.communitymanagement.service.HouseService;
import com.woniu.communitymanagement.mapper.HouseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author QIQI
* @description 针对表【house(房屋表)】的数据库操作Service实现
* @createDate 2022-11-29 09:57:53
*/
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House>
    implements HouseService{
    @Autowired(required = false)
    private HouseMapper houseMapper;
    @Override
    public HttpResult getHouseList(String number, Integer pageIndex, Integer pageSize) {
        HttpResult httpResult=null;
        List<House> houses = houseMapper.selectAll(number, (pageIndex - 1) * pageSize, pageSize);
        Integer count = houseMapper.selectAllCount(number);
        if(houses!=null&&houses.size()>0){
            httpResult=new HttpResult(houses,count,200,"查询成功");
        }else{
            if(pageIndex>1){
                houses = houseMapper.selectAll(number, (pageIndex - 1) * pageSize, pageSize);
                httpResult=new HttpResult(houses,count,200,"查询成功");
            }else{
                httpResult=new HttpResult(null,null,500,"查询失败");
            }
        }
        return httpResult;
    }

    @Override
    public HttpResult deleteById(Integer id) {
        HttpResult httpResult=null;
        boolean isDelete = removeById(id);
        if(isDelete){
            httpResult=new HttpResult(null,null,200,"删除成功");
        }else {
            httpResult=new HttpResult(null,null,500,"删除失败");
        }
        return httpResult;
    }

    /**
     * {
     *   "data": null,
     *   "pageTotal": null,
     *   "code": 200,
     *   "msg": "修改成功"
     * }
     * @param house 修改信息
     * @return
     */
    @Override
    public HttpResult updateHouseById(House house) {
        HttpResult httpResult=null;
        int count = houseMapper.updateById(house);
        if (count>0){
            httpResult=new HttpResult(null,null,200,"修改成功");
        }else{
            httpResult=new HttpResult(null,null,500,"修改失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult insertHouse(House house) {
        HttpResult httpResult=null;
        int count = houseMapper.insert(house);
        if(count>0){
            httpResult=new HttpResult(null,null,200,"添加成功");
        }else{
            httpResult=new HttpResult(null,null,500,"添加失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult selectOne(Integer id) {
        HttpResult httpResult=null;
        House house = houseMapper.selectHouseOne(id);
        if(house!=null){
            httpResult=new HttpResult(house,1,200,"查询成功");
        }else{
            httpResult=new HttpResult(null,null,500,"查询失败");
        }
        return httpResult;
    }

    @Override
    public HttpResult selectAll() {
        HttpResult httpResult=null;
        List<House> houses = houseMapper.selectHouseAll();
        if(houses!=null&&houses.size()>0){
            httpResult=new HttpResult(houses,houses.size(),200,"查询成功");
        }else {
            httpResult=new HttpResult(null,null,500,"查询失败");
        }
        return httpResult;
    }
}




